//
//  PainterCrosshairs.m
//  ChartRefactor
//
//  Created by CaoJun on 15/3/16.
//  Copyright (c) 2015年 CaoJun. All rights reserved.
//

#import "PainterCrosshairs.h"

using namespace ytx;

@implementation PainterCrosshairs

- (void)drawCrosshairsY:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();

    [ctx setStrokeStyle:[NSString stringWithUTF8String: style.crosshairsColor.c_str() ] ];
    [ctx setLineWidth:style.crosshairsWidth];
    [ctx drawLineWithX1:rect.getBeginX() withY1: px withX2:rect.getEndX() withY2: px];

}

- (void)drawCrosshairsYText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px withValue:(double) value withText:(NSString *) text
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.crosshairsTextFont.c_str()];
    
    CGRect backgroundRect = [ctx getTextRect:text withFont:font withSize:style.crosshairsTextSize];
    backgroundRect.size.width+=2;
    backgroundRect.size.height+=2;
    backgroundRect.origin.x = rect.getBeginX();
    backgroundRect.origin.y = px-backgroundRect.size.height*0.5;
    
    [ctx fillRect:backgroundRect withFillColor:[NSString stringWithUTF8String:style.crosshairsTextBackgorundColor.c_str()]];
    
    [ctx fillText:text withX:rect.getBeginX()+1 withY:px withColor:[NSString stringWithUTF8String:style.crosshairsTextColor.c_str()] withFont:font withSize:style.crosshairsTextSize withAnchor:LeftMiddle];

}
- (void)drawCrosshairsYTextInKlinePainter:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px withValue:(double) value withTimeType:(NSInteger) type withIndex:(NSInteger) index
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();
    
    NSString * font = [NSString stringWithUTF8String:style.crosshairsTextFont.c_str()];
    KLineData * secondData = (KLineData *)indexPosition->getData();
    int startIndex = indexPosition->getStartDataIndex();
    
    YTXTime dateTime = secondData->getTradedate((int)index +startIndex);
    if (dateTime.is_not_a_date_time()) {
        return;
    }
    
    std::string stringTemp = dateTime.toStringYYYYMMddHHmmss();
    stringTemp.insert(4, "/").insert(7, "/");
    if (LineType::isMinuteLine(secondData->quotationType)) {
        stringTemp = stringTemp.erase(16,17).erase(0, 5);
    }else if (!LineType::isKLine(secondData->quotationType)) {
        stringTemp = stringTemp.erase(16,17).erase(0, 10);
    } else {
        stringTemp = stringTemp.erase(10, 10);
    }
    
    NSString *dateTimeStr = [NSString stringWithUTF8String:stringTemp.c_str()];
    CGRect backgroundRect = [ctx getTextRect:dateTimeStr withFont:font withSize:style.crosshairsTextSize];
    backgroundRect.size.width+=2;
    backgroundRect.origin.x = px-backgroundRect.size.width*0.5;
    backgroundRect.origin.y = rect.getEndY()-backgroundRect.size.height;
    
    CGRect restrictRect = CGRectZero;
    CGFloat restrictX = 0.0;
    if (backgroundRect.origin.x < rect.getBeginX()) {
        restrictRect = (CGRect){rect.getBeginX(), backgroundRect.origin.y, backgroundRect.size};
        restrictX = rect.getBeginX()+backgroundRect.size.width/2.0;
    } else if (backgroundRect.origin.x+backgroundRect.size.width > rect.getRectWidth()+rect.getBeginX()) {
        restrictRect = (CGRect){rect.getRectWidth()+rect.getBeginX()-backgroundRect.size.width, backgroundRect.origin.y, backgroundRect.size};
        restrictX = rect.getRectWidth()+rect.getBeginX()-backgroundRect.size.width/2.0;
    } else {
        restrictRect = backgroundRect;
        restrictX = px+1;
    }
    [ctx fillRect:restrictRect withFillColor:[NSString stringWithUTF8String:style.crosshairsTextBackgorundColor.c_str()]];
    [ctx fillText:dateTimeStr withX:restrictX withY:rect.getEndY() withColor:[NSString stringWithUTF8String:style.crosshairsTextColor.c_str()] withFont:font withSize:style.crosshairsTextSize withAnchor:CenterBottom];
}

- (void)drawCrosshairsXText:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ytx::ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px withIndex:(NSInteger) index
{

}

- (void)drawCrosshairsX:(CGRect)cgrect withContext:(ChartCanvas *) ctx withViewModelIndexPosition:(ViewModelIndexPosition *) indexPosition withPx:(CGFloat) px
{
    YTXConfig * config = YTXConfig::getInstance();
    ytx::Style style = config->style;
    Rectangle & rect = indexPosition->getRectangle();

    [ctx setStrokeStyle:[NSString stringWithUTF8String: style.crosshairsColor.c_str() ] ];
    [ctx setLineWidth:style.crosshairsWidth];
    [ctx drawLineWithX1:px withY1:rect.getBeginY() withX2:px withY2:rect.getEndY()];

}

@end
